package com.morehair.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Author  kkw
 * Date  2021/6/9 9:07
 * Version 1.0
 * Description :
 */
public class Md5Util {
    public static String getMd5(String code) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("md5");
        byte[] resultBytes = md5.digest(code.getBytes(StandardCharsets.UTF_8));
        StringBuffer stringBuffer = new StringBuffer();
        for (byte resultByte : resultBytes) {
            String result = Integer.toHexString(resultByte & 0xff);
            if (result.length() == 1) {
                stringBuffer.append(0);
            }
            stringBuffer.append(result);
        }
        return stringBuffer.toString();
    }
}
